//
//  PilotSkillsVC.m
//  EVETracker
//
//  Created by Scott Guyer on 8/3/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "Character.h"
#import "SkillVC.h"
#import "SkillListVC.h"
#import "SkillGroupVC.h"
#import "RootPilotViewController.h"
#import "PilotSkillsVC.h"
#import "defs.h"


@implementation PilotSkillsVC

@synthesize navVC, rootPilotVC;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) 
	{
		// Initialization code

	}
	
	return self;
}



- (void) updateViewWithPilot:(Character*)p 
{
	pilot = p;
		
	if ( skillGroupVC == nil )
	{
		skillGroupVC = [[SkillGroupVC alloc] initWithNibName:@"SkillGroupView" bundle:nil];
		UIBarButtonItem* tmpButton = [[UIBarButtonItem alloc] 		
								  initWithTitle:@"Back" 
								  style:UIBarButtonItemStyleBordered
								  target:self 
								  action:@selector(doneButton:)];
		skillGroupVC.navigationItem.leftBarButtonItem = tmpButton;
		[tmpButton release];
		skillGroupVC.navVC = navVC;
	}
	
	// NOTE: there was a time when I was just keeping the one skillGroupVC around and just updating its
	// values.  Unfortunately, UINavigationBar/Controller would not allow me to update the title for the
	// bar item.  Really weird.  So now I nuke the old skillGroupVC and recreate.  :'(
	
	skillGroupVC.title = [NSString stringWithFormat:@"%@'s Skills", pilot.name];
	[skillGroupVC setPilotModeWithPilot:pilot];
	
	[navVC pushViewController:skillGroupVC animated:NO];
}


- (void) doneButton:(id)sender
{
	[rootPilotVC showPilots];
	[navVC popViewControllerAnimated:NO];
	// [skillGroupVC release]; // this VC was on the navVC stack.  
	// But the automatic "Back" button pops the view, but doesn't release it.  Hence, the 
	// extra one here.  But doing two releases corrupts the NavVC stack.  So, looks like I
	// have to eat the leak.
 	WIPE( skillGroupVC)

}


- (void)viewDidLoad 
{
	CGRect f = navVC.view.frame;
	f.origin.y = 0;
	f.size.height = 412;
	navVC.view.frame = f;
	[self.view addSubview:navVC.view];
	
	[navVC.navigationBar setBarStyle:UIBarStyleBlackOpaque];
	
	[self.view addSubview:navVC.view];
}


- (void) viewWillAppear:(BOOL)animated
{
	NSLog(@"%s", __FUNCTION__);
	[navVC viewWillAppear:animated];
	[skillGroupVC viewWillAppear:animated];
}

- (void) viewDidAppear:(BOOL)animated
{
	NSLog(@"%s", __FUNCTION__);	
	[navVC viewDidAppear:animated];
	[skillGroupVC viewDidAppear:animated];
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
}

- (void) jenny
{
	NSLog(@"%s", __FUNCTION__);

	if ( skillGroupVC != nil )
	{
		[skillGroupVC jenny];
		WIPE( skillGroupVC)
	}
}


- (void)dealloc {
	NSLog(@"%s 0x%x", __FUNCTION__, self);
	
	WIPE( navVC)
	WIPE( rootPilotVC)
	WIPE( skillGroupVC)
	
	[super dealloc];
}


@end
